package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wd.health.R;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gyf.immersionbar.ImmersionBar;
import com.wd.health.model.bean.SfSignInBean;
import com.wd.health.model.bean.SignInBean;
import com.wd.health.utils.RetrofitManager;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MineActivity extends AppCompatActivity implements View.OnClickListener {

    private SimpleDraweeView sdv_mine;
    private TextView tv_mine_name;
    private ImageView img_back;
    private LinearLayout lin_shezhi;
    private LinearLayout lin_mine_wenzhen;
    private LinearLayout lin_money;
    private LinearLayout lin_history_wenzhen;
    private Button btn_slgnin;
    private LinearLayout lin_usertask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }

    private void initData() {
        SharedPreferences user = getSharedPreferences("user", Context.MODE_PRIVATE);
        String headPic = user.getString("headPic", "");
        String nickName = user.getString("nickName", "");
        sdv_mine.setImageURI(headPic);
        tv_mine_name.setText(nickName);
    }

    private void initView() {
        sdv_mine = findViewById(R.id.sdv_mine);
        lin_shezhi = findViewById(R.id.lin_shezhi);
        tv_mine_name = findViewById(R.id.tv_mine_name);
        lin_mine_wenzhen = findViewById(R.id.lin_mine_wenzhen);
        lin_money = findViewById(R.id.lin_money);
        img_back = findViewById(R.id.img_back);
        lin_history_wenzhen = findViewById(R.id.lin_history_wenzhen);
        btn_slgnin = findViewById(R.id.btn_slgnin);
        lin_usertask = findViewById(R.id.lin_usertask);
        lin_mine_wenzhen.setOnClickListener(this);
        sdv_mine.setOnClickListener(this);
        lin_shezhi.setOnClickListener(this);
        img_back.setOnClickListener(this);
        lin_money.setOnClickListener(this);
        lin_history_wenzhen.setOnClickListener(this);
        btn_slgnin.setOnClickListener(this);
        lin_usertask.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lin_mine_wenzhen:
                Intent intent = new Intent(MineActivity.this, DqwzActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.sdv_mine:
                Intent intent1 = new Intent(MineActivity.this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.lin_shezhi:
                Intent intent3 = new Intent(MineActivity.this, SetActivity.class);
                startActivity(intent3);
                finish();
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.lin_money:
                Intent intent4 = new Intent(MineActivity.this, MoneyActivity.class);
                startActivity(intent4);
                finish();
                break;
            case R.id.lin_history_wenzhen:
                Intent intent5 = new Intent(MineActivity.this, HistoryWzActivity.class);
                startActivity(intent5);
                finish();
                break;
            case R.id.btn_slgnin:
                Judgment();
                break;
            case R.id.lin_usertask:
                Intent intent6 = new Intent(MineActivity.this, UserTaskActivity.class);
                startActivity(intent6);
                finish();
                break;
        }
    }

    private void Judgment() {
        RetrofitManager.getInstance().getSigninData(new Observer<SfSignInBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SfSignInBean sfSignInBean) {
                int result = sfSignInBean.getResult();
                if (result == 2){
                    initSignIn();
                }else {
                    Toast.makeText(MineActivity.this, "今天已签到", Toast.LENGTH_SHORT).show();
                    btn_slgnin.setText("已签到");
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initSignIn() {
        RetrofitManager.getInstance().postSignInData(new Observer<SignInBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SignInBean signInBean) {
                if (signInBean.getMessage().equals("签到成功")){
                    Toast.makeText(MineActivity.this, "签到成功", Toast.LENGTH_SHORT).show();
                    btn_slgnin.setText("已签到");
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}